package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishListVo;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishPage = dishMapper.pageDish(dishPageQueryDTO);
        System.out.println(dishPage);

        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    @Transactional
    @Override
    public void addDishAndFlavo(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setCreateUser(BaseContext.getCurrentId());
        dishMapper.add(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);

        }

        dishFlavorMapper.add(flavors);

    }

    @Override
    public void openOrClose(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateTime(LocalDateTime.now());

        dishMapper.Update(dish);
    }

    @Override
    public DishVO dishById(Long id) {

        DishVO dishVO = dishMapper.dishById(id);
        List<DishFlavor> flavors = dishFlavorMapper.dishFlavors(id);
dishVO.setFlavors(flavors);
        return dishVO;
    }
@Transactional
    @Override
    public void dishUpdate(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dishMapper.Update(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long id = dishDTO.getId();
        flavors.forEach(e -> e.setDishId(dishDTO.getId()));

        if (flavors != null && flavors.size() > 0) {

                dishFlavorMapper.DeleteById(id);
            dishFlavorMapper.add(flavors);
            }

        }

    @Override
    public void dishDelete(Integer[] ids) {

        dishMapper.dishDelete(ids);
        dishFlavorMapper.dishFlavorsDelete(ids);
    }

    @Override
    public List<Dish> categoryByIdList(Long categoryId) {

List<Dish> list=dishMapper.categoryByIdList(categoryId);

        return list;
    }
    @Override
    public List<DishVO> categoryByIdListAndFlavor(Long categoryId) {
        List<DishVO> dishVOS=new ArrayList<>();

        List<Dish> list=dishMapper.categoryByIdList(categoryId);
        for (Dish dish : list) {
            DishVO dishVO = dishById(dish.getId());
            dishVOS.add(dishVO);

        }


        return dishVOS;
    }


}